package com.ruoyi.driver.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

//import org.activiti.engine.RuntimeService;

import com.ruoyi.driver.domain.TbDriverTurnover;
import com.ruoyi.driver.ocrUnit.BaiduOcrUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.driver.domain.TbDriver;
import com.ruoyi.driver.service.ITbDriverService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 司机管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@RestController
@RequestMapping("/driver")
public class TbDriverController extends BaseController
{
    @Autowired
    private ITbDriverService tbDriverService;
//    @Autowired
//    private BaiduFaceService faceService;


    /**
     * 查询司机管理列表
     */
    @RequiresPermissions("driver:driver:list")
    @GetMapping("/list")
    public TableDataInfo list(TbDriver tbDriver)
    {
        startPage();

        List<TbDriver> list = tbDriverService.selectTbDriverList(tbDriver);
        return getDataTable(list);
    }
    @RequiresPermissions("driver:driver:list")
    @GetMapping("/getTurnoverByDriverId/{driverId}")
    public TableDataInfo getTurnoverByDriverId(@PathVariable("driverId") Integer driverId)
    {
        startPage();

        List<TbDriverTurnover> list = tbDriverService.getTurnoverByDriverId(driverId);
        return getDataTable(list);
    }


    /**
     * 导出司机管理列表
     */
    @RequiresPermissions("driver:driver:export")
    @Log(title = "司机管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbDriver tbDriver)
    {
        List<TbDriver> list = tbDriverService.selectTbDriverList(tbDriver);
        ExcelUtil<TbDriver> util = new ExcelUtil<TbDriver>(TbDriver.class);
        util.exportExcel(response, list, "司机管理数据");
    }

    /**
     * 获取司机管理详细信息
     */
    @RequiresPermissions("driver:driver:query")
    @GetMapping(value = "/{driverId}")
    public AjaxResult getInfo(@PathVariable("driverId") Integer driverId)
    {
        return success(tbDriverService.selectTbDriverByDriverId(driverId));
    }

    /**
     * 新增司机管理
     */
    @RequiresPermissions("driver:driver:add")
    @Log(title = "司机管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbDriver tbDriver)
    {
        return toAjax(tbDriverService.insertTbDriver(tbDriver));
    }

    /**
     * 修改司机管理
     */
    @RequiresPermissions("driver:driver:edit")
    @Log(title = "司机管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbDriver tbDriver)
    {
        return toAjax(tbDriverService.updateTbDriver(tbDriver));
    }

    /**
     * 删除司机管理
     */
    @RequiresPermissions("driver:driver:remove")
    @Log(title = "司机管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{driverIds}")
    public AjaxResult remove(@PathVariable Integer[] driverIds)
    {
        return toAjax(tbDriverService.deleteTbDriverByDriverIds(driverIds));
    }
    /**
     * 司机审核接口
     */

    @Log(title = "司机管理", businessType = BusinessType.DELETE)
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody TbDriver tbDriver)
    {
        return tbDriverService.updateStatus(tbDriver);
    }


    /**
     * 身份证 OCR 识别
     * @param file 上传的图片文件
     * @param side 正面/反面 front/back
     * @return 识别结果
     */
    @PostMapping("/idcard")
    public AjaxResult ocrIdCard(@RequestParam("file") MultipartFile file,
                                @RequestParam(value = "side", defaultValue = "front") String side) {
        try {
            byte[] bytes = file.getBytes();
            String base64 = java.util.Base64.getEncoder().encodeToString(bytes);

            Map<String, Object> result = BaiduOcrUtil.identifyIdCard(base64);
            return AjaxResult.success(result);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("身份证识别失败：" + e.getMessage());
        }
    }

    /**
     * 驾驶证 OCR 识别
     */
    @PostMapping("/driver-license")
    public AjaxResult ocrDriverLicense(@RequestParam("file") MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            String base64 = java.util.Base64.getEncoder().encodeToString(bytes);

            Map<String, Object> result = BaiduOcrUtil.identifyDriverLicense(base64);
            return AjaxResult.success(result);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("驾驶证识别失败：" + e.getMessage());
        }
    }

    /**
     * 行驶证 OCR 识别
     */
    @PostMapping("/vehicle-license")
    public AjaxResult ocrVehicleLicense(@RequestParam("file") MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            String base64 = java.util.Base64.getEncoder().encodeToString(bytes);

            Map<String, Object> result = BaiduOcrUtil.identifyVehicleLicense(base64);
            return AjaxResult.success(result);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("行驶证识别失败：" + e.getMessage());
        }
    }


}





